/**
 * 
 */
package com.wgo.precise.client.ui.view.editor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Tree;

/**
 * @author petterei
 *
 * @version $Id: ViewerSectionLayout.java,v 1.1 2006-01-23 19:58:38 petterei Exp $
 */
public class ViewerSectionLayout extends Layout /*implements ILayoutExtension */{
	private IViewerSectionPart part = null;
	public int marginSize = IModelFormPart.BORDER_SIZE;

	public ViewerSectionLayout(IViewerSectionPart part){
		this.part = part;
	}
	@Override
	protected Point computeSize(@SuppressWarnings("unused") Composite composite, int wHint, int hHint, boolean flushCache) {
		Point result = null;
		Scrollable viewerControl = (Scrollable)part.getViewer().getControl();
		if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
			result = viewerControl.computeSize(wHint, hHint);
		} else if (wHint != SWT.DEFAULT && hHint == SWT.DEFAULT) {
			result = viewerControl.computeSize(wHint, hHint);
			result.y += 2 * marginSize;
		} else if (wHint == SWT.DEFAULT && hHint != SWT.DEFAULT) {
			result = viewerControl.computeSize(wHint, hHint);
			result.x += 2 * marginSize;
		} else if (viewerControl != null) {
//			Point viewerPoint = viewerControl.getSize() ;
			Rectangle viewerRectangle = viewerControl.getClientArea();
			//			int actualHeight = viewerControl.getSize().y + viewerControl.getBorderWidth();
			hHint = viewerRectangle.height ;
//			hHint = hHint > actualHeight ? hHint : actualHeight ;
//			Rectangle clientAreaRectangle = part.getSection().getClientArea();
//			int clientAreaHeight = clientAreaRectangle.height;
//			int clientAreaWidth = clientAreaRectangle.width;
//			Control client = part.getSection().getClient();
//			Point clientPoint = client.getSize();
			result = viewerControl.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
//			Point result1 = viewerControl.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
//			Point result2 = viewerControl.computeSize(wHint, hHint, flushCache);
			if (viewerControl instanceof Tree) {
				Tree tree = (Tree)viewerControl;
//				int border = tree.getBorderWidth();
//				int actualHeight = viewerPoint.y - (border * 2);
				result = tree.computeSize(wHint, 0 , flushCache);
//				result.y += tree.getHeaderHeight();
//				result.y += tree.getItemHeight();
				System.out.println("--");
				System.out.println("Height: " + result.y);
				System.out.println("itemCount: " + tree.getItemCount());
				System.out.println("itemHeight" + tree.getItemHeight());
//				System.out.println("" + tree.);
//				System.out.println("" + tree.);
			}
//			result.x += 2 * marginSize;
//			result.y += 2 * marginSize;
		} else {
			result = new Point(0, 0);
		}
		return result;
	}
	@Override
	protected void layout(Composite composite, @SuppressWarnings("unused") boolean flushCache) {
		if (part.getSection() != null && null != part.getViewer()) {
			Rectangle rectangle = composite.getClientArea();
			rectangle.x += marginSize;
			rectangle.y += marginSize;
			rectangle.height = rectangle.height - 2 * marginSize;
			rectangle.width = rectangle.width - 2 * marginSize;
//			DetailsSheetViewer v = (DetailsSheetViewer) part.getViewer();
			part.getViewer().getControl().setBounds(rectangle);
		}
	}
}
